/**
 * Mar 27, 2010
 */
package org.tomale.id.power.customers.inquiry.ui;

import java.util.List;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.tomale.id.ProviderException;
import org.tomale.id.common.SearchFilterType;
import org.tomale.id.power.customers.Customer;
import org.tomale.id.power.customers.inquiry.Activator;
import org.tomale.id.power.customers.inquiry.CustomerInquiry;

/**
 * @author ferd
 *
 */
public class CustomerBrowser extends ViewPart {

	public final static String VIEW_ID = "org.tomale.id.power.customers.inquiry.browser";
	
	Composite _simple;
	Composite _advanced;
	
	Combo _type;
	Text _sz;
	Button _find;
	
	TableViewer _vwr;
	
	CustomerInquiry _inquiry;
	List<SearchFilterType> _types;
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		parent.setLayout(layout);
		
		_simple = new Composite(parent, SWT.NONE);
		_simple.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				true,false));
		GridLayout simpleLayout = new GridLayout();
		simpleLayout.numColumns = 3;
		_simple.setLayout(simpleLayout);
		
		_type = new Combo(_simple, SWT.DROP_DOWN | SWT.READ_ONLY);
		_type.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				false,false));
		initTypes();
		
		_sz = new Text(_simple, SWT.BORDER | SWT.SEARCH);
		_sz.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				true,false));
		
		_find = new Button(_simple, SWT.PUSH);
		_find.setImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/actions/search.gif").createImage());
		_find.setLayoutData(new GridData(SWT.FILL,SWT.BEGINNING,
				false,
				false));
		_find.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				doFind();
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				
			}
		});
		
		_vwr = new TableViewer(parent);
		_vwr.setContentProvider(new ContentProvider());
		_vwr.setLabelProvider(new LabelProvider());
		initTable();
		
		if(_type.getItemCount() == 0){
//			_type.setEnabled(false);
//			_sz.setEnabled(false);
//			_find.setEnabled(false);
			_sz.setEnabled(false);
			_simple.setEnabled(false);
			_vwr.getTable().setEnabled(false);
		}
		
		getSite().setSelectionProvider(_vwr);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		_sz.setFocus();
	}
	
	private void setErrorMessage(final String message){
		getViewSite().getActionBars().getStatusLineManager().setErrorMessage(message);
	}
	
	private CustomerInquiry getInquiry(){
		if(_inquiry == null){
			_inquiry = CustomerInquiry.getInstance();
		}
		return _inquiry;
	}
	
	private void initTypes(){
		try {
			CustomerInquiry inquiry = getInquiry();
			_types = inquiry.getFilterTypes();
			for(SearchFilterType sf : _types){
				_type.add(sf.getName());
			}
		}catch(ProviderException e){
			setErrorMessage(e.getMessage());
		}
	}
	
	private void initTable(){
		Table tbl = _vwr.getTable();
		tbl.setHeaderVisible(true);
		tbl.setLinesVisible(true);
		tbl.setLayoutData(new GridData(SWT.FILL,SWT.FILL,
				true,true));
		
		TableColumn col;
		
		col = new TableColumn(tbl, SWT.NONE);
		col.setText("");
		col.setToolTipText("Customer Status");
		col.setWidth(20);
		col.setResizable(false);
		col.setMoveable(true);
		
		col = new TableColumn(tbl, SWT.NONE);
		col.setText("Name");
		col.setToolTipText("Customer Name");
		col.setWidth(100);
		col.setResizable(true);
		col.setMoveable(true);
		
		col = new TableColumn(tbl, SWT.NONE);
		col.setText("Address");
		col.setToolTipText("Customer Address");
		col.setWidth(100);
		col.setResizable(true);
		col.setMoveable(true);
	}
	
	private boolean valid(){
		return true;
	}
	
	private void doFind(){
		if(valid()){
			// TODO find
		}
	}
	
	public void setAdvancedSearch(final boolean enabled){
		// TODO more work on this
		// row should also collapse
		_simple.setVisible(!enabled);
	}
	
	public void setCustomers(final List<Customer> customers){
		// TODO
	}
	
	private class ContentProvider implements IStructuredContentProvider {

		@Override
		public Object[] getElements(Object inputElement) {
			return ((List<Customer>) inputElement).toArray();
		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	private class LabelProvider extends StyledCellLabelProvider
		implements ITableLabelProvider {

		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			// TODO Auto-generated method stub
			return null;
		}
		
	}

}
